<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profileObj.photo"
            @click="imageClickFn"
          />
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profileObj.name"
        @click="nameClickFn"
      />
      <van-dialog
        v-model="show"
        title="标题"
        show-cancel-button
        :before-close="beforeCloseFn"
      >
        <input type="text" v-model="inputUserName" v-foo />
      </van-dialog>
      <van-cell title="生日" is-link :value="profileObj.birthday" @click="birthdayClickFn" />
    </van-cell-group>
    <!-- 时间选择器 -->
    <van-popup v-model="dateTimePickerShow" round position="bottom" :style="{ height: '50%' }">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel = 'dateCancelFn'
        @confirm = 'confirmFn'
      />
    </van-popup>
  </div>
</template>

<script>
import { userProfileAPI, updateUserPhotoAPI, updateUserProfileAPI } from '@/api'
import Notify from '@/ui/Notify'
import { formatDate } from '@/utils/date'
import { mapMutations } from 'vuex'
export default {
  name: 'UserEdit',
  data() {
    return {
      profileObj: {},
      show: false,
      inputUserName: '',
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(),
      dateTimePickerShow: false
    }
  },
  async created() {
    const res = await userProfileAPI()
    console.log(res)
    this.profileObj = res.data.data
    this.SET_USERPHOTO(res.data.data.photo)
  },
  methods: {
    ...mapMutations(['SET_USERPHOTO', 'SET_USERNAME']),
    //   修改用户的图片
    async onFileChange(e) {
      if (e.target.files.length === 0) return
      console.log(e.target.files[0])
      const theFd = new FormData()
      theFd.append('photo', e.target.files[0])
      const res = await updateUserPhotoAPI(theFd)
      console.log(res)
      this.profileObj.photo = res.data.data.photo
    },
    imageClickFn() {
      this.$refs.iptFile.click()
    },
    // 点击用户名字修改
    nameClickFn() {
      this.show = true
      this.SET_USERNAME(this.profileObj.name)
      this.inputUserName = this.profileObj.name
    },
    // 姓名 - 确认框 - 关闭前的回调函数
    async beforeCloseFn(action, done) {
      if (action === 'confirm') {
        //   校验用户名称
        const reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.inputUserName) === true) {
          await updateUserProfileAPI({
            name: this.inputUserName
          })
          this.profileObj.name = this.inputUserName
          done()
        } else {
          Notify({ type: 'warning', message: '密码格式不符合' })
          done(false)
        }
      } else {
        done()
      }
    },
    // 生日 - 点击显示默认的生日
    birthdayClickFn() {
      this.dateTimePickerShow = true
      this.currentDate = new Date(this.profileObj.birthday)
    },
    // 时间选择器取消按钮
    dateCancelFn() {
      this.dateTimePickerShow = false
    },
    // 时间选择器确认按钮 - 改变生日
    async confirmFn() {
      await updateUserProfileAPI({
        birthday: formatDate(this.currentDate)
      })
      this.profileObj.birthday = formatDate(this.currentDate)
      this.dateTimePickerShow = false
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .van-dialog__content {
  text-align: center;
  input {
    padding: 0px;
    border: none;
    outline: none;
    text-align: center;
  }
}
</style>
